package org.vectrics.web.struts;

/**
 * @author Mike Moore
 */

import org.apache.log4j.Category;
import org.vectrics.common.html.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.user.Person;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import org.vectrics.company.Company;
import org.vectrics.company.CompanySelectCriteria;



public class UserCompaniesTag extends TagSupport {
    static private Category log = Category.getInstance(UserCompaniesTag.class);
    private String userId = null;
    private String contextPath = null;
    
    public UserCompaniesTag() {
    	//
    }


	public int doStartTag() throws JspException  {
		log.debug("doStartTag() - render a combo box with all companies listed");
		StringBuffer html = new StringBuffer();
		
		log.debug("doStartTag() called");
		HttpServletRequest request = (HttpServletRequest)super.pageContext.getRequest();
		contextPath = request.getContextPath();
		try {
			Person person = CoreServiceLocator.getPersonService().findPersonById(new Long(userId));
			Table table = new BorderTable();
			CompanySelectCriteria criteria = new CompanySelectCriteria();
			Collection companys = CoreServiceLocator.getCompanyService().findCompanies(criteria, 0, 100);
			Iterator iter = companys.iterator();
			while (iter.hasNext()) {
				Company company = (Company)iter.next();
				if (company != null) {
					makeCompanyUserEntry(table, company, person);
				}
			}
			html.append(table.toString());
			
		}
		catch (Exception ex) {
			log.error("Exception displaying companies", ex);
		}
		
		JspWriter writer = pageContext.getOut();
		try {
			writer.print(html.toString());
		}
		catch (IOException ex) {
			throw new JspException("IOException attempting to write tag to output. " + ex.getMessage());
		}
		return(EVAL_PAGE);
	}

	private void makeCompanyUserEntry(Table entryTable, Company company, Person person) {
		Row row = entryTable.addRow();
		Cell cell = row.addCell();
		cell.addElement(TextHelper.getFont("stdFont", company.getName()));
		
		cell = row.addCell();
		String formHtml = "";
		formHtml += "<form name=\"companyUserForm\" method=\"POST\" action=\"" + contextPath 
					+ "/admin/jobs/candidate/interview/save.do\">";
		formHtml += "  <input type=\"text\" name=\"title\" maxlength=\"32\" value=\"\" class=\"formEditClass\">";
		formHtml += "  <input type=\"hidden\" name=\"companyId\" value=\"" + company.getId() + "\">";
		formHtml += "  <input type=\"hidden\" name=\"userId\" value=\"" + person.getId() + "\">";
		formHtml += "  <input name=\"submit\" type=\"submit\" value=\"Submit\" class=\"formButton\">";
		formHtml += "</form>";
		cell.addElement(formHtml);
	}

	/**
	 * @return Returns the userId.
	 */
	public String getUserId() {
		return userId;
	}

	/**
	 * @param userId The userId to set.
	 */
	public void setUserId(String userId) {
		this.userId = userId;
	}

}
